package com.zzj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzj.bean.PlanBean;
import com.zzj.bean.ProductBean;
import com.zzj.dao.OrdersDao;
import com.zzj.dao.PlanDao;
import com.zzj.dao.ProductDao;
import com.zzj.dao.ShapeDao;
import com.zzj.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductDao productDao;
    @Resource
    private OrdersDao ordersDao;
    @Resource
    private PlanDao planDao;
    @Resource
    private ShapeDao shapeDao;

    @Override
    public PageInfo<ProductBean> getProductByOoid(int pageNum, int pageSize,String ooid) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductBean> products = productDao.getProductByOoid(ooid);
        return new PageInfo<ProductBean>(products);
    }

    @Override
    public Result addProduct(List<ProductBean> productBeans) {
        String ooid = null;
        for (ProductBean productBean : productBeans) {
            if (ooid == null){
                ooid = productBean.getOoid();
            }
            System.out.println("productBean = " + productBean);
            System.out.println(productBean.getPcount());
            productBean.setPstate("0");
            productBean.setOoid(ooid);
            // 添加产品的同时,添加打型表
            for (int i = 0; i < productBean.getPcount(); i++) {
                productDao.addProduct(productBean);
            }
            ordersDao.updateAmountByOoid(productBean.getOoid(), productBean.getPcount());

            PlanBean plan = new PlanBean();
            plan.setPamount(productBean.getPcount().toString());
            plan.setOoid(productBean.getOoid());
            plan.setPtype(productBean.getPtype());
            plan.setPcraft("A5");
            plan.setPstate("0");
            planDao.addPlan(plan);
        }
        return new Result();

        //return new Result(false, StatusCode.ERROR,"");
    }

    @Override
    public PageInfo<ProductBean> findAll(int pageNum,int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductBean> all = productDao.findAll();
        return new PageInfo<>(all);
    }

    @Override
    public PageInfo<ProductBean> findAllQualityByState(int pageNum,int pageSize,String pstate) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductBean> productBeans = productDao.findAllQualityByState(pstate);
        return new PageInfo<>(productBeans);
    }
}
